package com.yanqu.road.web.controller.usercenter.newadmin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.admin.AdminCommonDaoImpl;
import com.yanqu.road.entity.admin.AdminResult;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.config.Config;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.web.WebServer;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.*;

/**
 * 67.内服外充白名单
 * 67.内服白名单(外充限制)
 */
@RestController
@RequestMapping("/usercenter/admin")
public class CenterInnerUserController {

    @ResponseBody
    @RequestMapping(value = "innerUser/edit", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String editInnerUser(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        int type = jsonObject.getIntValue("type");

        JSONArray playerList = jsonObject.getJSONArray("playerList");

        Set<Long> serverSet = new HashSet<>();
        Set<Long> userSet = new HashSet<>();
        if(type == 0){
            //新增
            List<Map<String, Object>> dataMap = new ArrayList<>();
            for (int i = 0; i < playerList.size(); i++) {
                JSONObject object = playerList.getJSONObject(i);
                dataMap.add(object);
                serverSet.add(object.getLongValue("serverId"));
            }
            boolean b = new AdminCommonDaoImpl().batchAddInnerUser(dataMap);
            if(!b){
                return AdminResult.failMsg("新增失败").toJsonString();
            }
        }else {
            //删除
            for (int i = 0; i < playerList.size(); i++) {
                JSONObject object = playerList.getJSONObject(i);
                long userId = object.getLongValue("userId");
                userSet.add(userId);
                if(object.containsKey("serverId")){
                    serverSet.add(object.getLongValue("serverId"));
                }else {
                    serverSet.add(Long.valueOf(object.getString("userId").substring(0, 8)));
                }
            }
            if(!userSet.isEmpty()){
                boolean b = new AdminCommonDaoImpl().deleteInnerUser(new ArrayList<>(userSet));
                if(!b){
                    //return AdminResult.failMsg("删除失败").toJsonString();
                }
            }
        }

        List<ServerInfo> serverInfoList = new ArrayList<>();
        for (long sid : serverSet) {
            ServerInfo serverInfo = ServerListMgr.getServerInfo(sid);
            if (serverInfo == null) {
                continue;
            }
            serverInfoList.add(serverInfo);
        }
        //刷新
        String msg = CenterReloadHelper.serverMgrReload(serverInfoList, "InnerUserMgr", 10);
        return AdminResult.defaultInstance().setMsg(msg).toJsonString();
    }
}
